<?php

namespace App\model;

use Illuminate\Database\Eloquent\Model;
use App\Standard;
use App\City;
use Composer\Cache;
use App\WorkshopIndustry;
use App\Website;

/**
 * App\model\Land
 *
 * @property-read mixed $slider
 * @property-read mixed $standard
 * @property-read \App\LandUse $landUse
 * @mixin \Eloquent
 * @property int $id
 * @property string|null $code 土地编号
 * @property string|null $p_district_id
 * @property string|null $p_city_id 区域
 * @property string|null $p_province_id 区域1
 * @property string|null $title 标题
 * @property string|null $rent 租金
 * @property int|null $price 价格：25元/亩
 * @property string|null $certificate 土地证
 * @property string|null $area 面积
 * @property string|null $property 土地性质
 * @property string|null $use 规划用途
 * @property string|null $address 地址
 * @property string|null $standard_id 交付标准
 * @property string|null $property_cert 产权证：0为无，1为有
 * @property string|null $rest_term 剩余土地年限
 * @property string|null $owner_release 是否业主发布：0为不是1为是
 * @property int|null $click_count 点击量
 * @property int|null $call_count d电话拨打量
 * @property string|null $telephone 联系电话
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null $tag 标签
 * @property string|null $transaction_type 交易类型
 * @property string|null $is_recommend 是否推荐
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereCallCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereCertificate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereClickCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereIsRecommend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereOwnerRelease($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land wherePCityId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land wherePDistrictId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land wherePProvinceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereProperty($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land wherePropertyCert($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereRent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereRestTerm($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereSlider($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereStandardId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereTag($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereTelephone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereTransactionType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereUse($value)
 * @property int|null $area_id 面积范围
 * @property string|null $latitude 经度
 * @property string|null $longitude 纬度
 * @property string|null $logo 首图
 * @property-read \App\AreaLand|null $areaLand
 * @property-read \App\Broker|null $broker
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereAreaId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereLatitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereLogo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereLongitude($value)
 * @property string|null $uses 规划用途
 * @property string|null $descs
 * @property string|null $is_out
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereDescs($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereIsOut($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Land whereUses($value)
 */
class Land extends Model
{
    //
    protected $table="land";
    protected $appends=['standard'];
    public function getLogoAttribute($value)
    {
        if($value){
            if(strstr($value,'http')){
                return $value.'?imageView2/0/w/512/h/384';
            }else{
                return Storage::url($value).'?imageView2/0/w/512/h/384';
            }
        }
        $silder= explode(',',$this->attributes['slider']);
        $silder=array_filter($silder);
        $map=[];
        foreach ($silder as $k=> $v){
            if(!preg_match("/(\.mp4|\.avi|\.wmv|\.mkv|\.rmvb|\.3gp)/",$v)){
                return   $value.'?imageView2/0/w/512/h/384';
            }
            //$map[][]=config('filesystems.disks')[config('admin.upload.disk')]['url'].'/'.$v;
        }
    }
    public function getTagAttribute($value){
        return explode(',',$value);
    }
    public function getSliderAttribute($value){
        $silder= explode(',',$value);
        $silder=array_filter($silder);
        $map=[];
        foreach ($silder as $k=> $v){
            if(preg_match("/(\.mp4|\.avi|\.wmv|\.mkv|\.rmvb|\.3gp)/",$v)){
                $map['video'][]=$v.'?imageView2/0/w/828/h/640';
            }else{
                $webSite = Website::find(1);
                $url = config('admin.app_outurl1').'/'.$webSite->watermark.'?imageView2/2/w/360/h/250';
                $map['image'][]=$v.'?imageView2/2/w/960/h/720|watermark/1/image/'.\Qiniu\base64_urlSafeEncode($url).'/gravity/'.$webSite->watermark_position;
            }
            //$map[][]=config('filesystems.disks')[config('admin.upload.disk')]['url'].'/'.$v;
        }
        return $map;
    }



    public function getStandardAttribute(){
        $standard= \Illuminate\Support\Facades\Cache::remember('standard','3',function(){
            return Standard::pluck('name','id')->toArray();
        });
        $option=$this->attributes['standard_id'];

        $array=[];
        $option=explode(',',$option);
        foreach ($option as $v){
            if(isset($standard[$v])){
                $array[]=$standard[$v];
            }
        }
        return  $array;
    }
    public function landUse(){
        return $this->belongsTo(\App\LandUse::class,'uses');
    }
    public function areaLand(){
        return $this->belongsTo(\App\AreaLand::class,'area_id');
    }

    public function getPCityIdAttribute($value){
        return  City::find($value)->title ?? '';
    }

    public function getPDistrictIdAttribute($value){
        return  City::find($value)->title ?? '';
    }

    public function getPProvinceIdAttribute($value){
        return  City::find($value)->title ?? '';
    }

    public function broker(){
        return $this->belongsTo(\App\model\Broker::class,'telephone');
    }
}
